﻿using System;
using System.Web.Mvc;
using User.EF.Contracts;
using User.Web.Contracts.Model.Admin;
using User.Web.Models.Admin;
using User.Web.UI.Controllers.BaseControllers;
using User.Web.UI.Helper.RightsManagement;

namespace User.Web.UI.Controllers
{
    /// <summary>
    /// Standard Controller für die Adminseiten.
    /// </summary>
    [CustomAuthorize]
    public class AdminController : ControllerBaseUser
    {
        #region Member
        /// <summary>
        /// User Repository für alle allgemeinen Abfragen fürs Web.
        /// </summary>
        private readonly IWebQuerySummary _webQueries;
        #endregion

        #region Initialize
        public AdminController(IWebQuerySummary webQueries)
        {
            if (webQueries == null)
            {
                throw new NullReferenceException("WebQuery is Null");
            }

            _webQueries = webQueries;
        }
        #endregion

        /// <summary>
        /// Anzeigen der Memberliste
        /// </summary>
        public ActionResult MemberList()
        {
            IMemberListApiModel apiModel = new MemberListApiModel();
            return View("MemberList", apiModel);
        }

        /// <summary>
        /// Anzeigen der Seite mit den Mitgliederdetails
        /// </summary>
        public ActionResult MemberDetails(long id)
        {
            MemberDetailsModel model = new MemberDetailsModel();
            model.SetCommunication(Logger, AddMessage);
            //Laden unserer Modeldaten
            if (model.Load(_webQueries, id))
            {
                return View("MemberDetails", model);
            }

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Seite zum Bearbeiten der Mitgliederdaten
        /// </summary>
        public ActionResult MemberEditAdmin(long id)
        {
            MemberEditAdminModel model = new MemberEditAdminModel();
            model.SetCommunication(Logger, AddMessage);
            if (model.Load(_webQueries, id))
            {
                return View("MemberEditAdmin", model);
                
            }

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Seite zum Bearbeiten der Mitgliederdaten
        /// </summary>
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult MemberEditAdmin(MemberEditAdminModel model, long id)
        {
            if (ModelState.IsValid)
            {
                model.SetCommunication(Logger, AddMessage);
                if (model.Save(_webQueries, id))
                {
                    return RedirectToAction("MemberEditAdmin", "Admin");
                }
            }

            return View("MemberEditAdmin", model);
        }

        /// <summary>
        /// Bearbeiten/setzten der passenden Mitarbeiterrechte
        /// </summary>
        public ActionResult MemberRights(long id)
        {
            MemberRightModel model = new MemberRightModel();
            model.SetCommunication(Logger, AddMessage);
            model.Load(_webQueries, id);
            return View("MemberRights", model);
        }
    }
}